Ext.define('jwawa.erp.cfg.searchPage.SearchItemModel', {
    extend: 'jwawa.erp.framework.JwawaModel',

    constructor: function(config){
        var me = this;

        Ext.apply(me, config);
        me.callParent(arguments);
        me.addEvents('saveSearchItem');
    },

    delSearchItem: function(itemId, opt){
        "use strict";
        var me = this,
            optional = me.buildOpt(opt);

        Jwawa.framework.JwawaConnection.request({
            url      : '/search-page/del-search-item-by-id',
            async    : true,
            jsonData: {id: itemId},
            success : function(response) {
                optional['success'](itemId, response);
            },
            failure:function(response){
                optional['failure'](itemId);
            },
            callback: function(options, success, response){
                optional['callback'](itemId);
            }
        });
    },

    saveSearchItem: function(searchItem, opt){
        "use strict";
        var me = this,
            optional = me.buildOpt(opt);

        Jwawa.framework.JwawaConnection.request({
            url      : '/search-page/save-search-item',
            async    : true,
            jsonData: searchItem,
            success : function(response) {
                var searchItem = response['data'];
                optional['success'](searchItem);

                me.fireEvent('saveSearchItem',me, searchItem);
            },
            failure:function(response){
                optional['failure'](response);
            },
            callback: function(options, success, response){
                optional['callback'](options);
            }
        });
    },

    changeSearchItemSeq: function(curItem, opteratorFlag, opt){
        "use strict";
        var me = this,
            optional = me.buildOpt(opt);

        if(curItem == null){return;}

        Jwawa.framework.JwawaConnection.request({
            url      : '/search-page/change-search-item-seq',
            async    : true,
            jsonData: {
                searchItemId: curItem.getId(),
                optFlag: opteratorFlag
            },
            success : function(response) {
                optional['success']();
            },

            callback: function(options, success, response){
                optional['callback'](response);
            }
        });
    }
});